<?php

namespace app\common\model\mall\oper;

use think\Model;

class Nav extends Model
{
    // 表名
    protected $name = 'mall_oper_nav';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    
    // 追加属性
    protected $append = [
        'navType_text',
        'isOpen_text'
    ];

    public function getNavtypeList()
    {
        return ['0' => __('Navtype 0'),'1' => __('Navtype 1')];
    }     

    public function getIsopenList()
    {
        return ['0' => __('Isopen 0'),'1' => __('Isopen 1')];
    }     


    public function getNavtypeTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['navType']) ? $data['navType'] : '');
        $list = $this->getNavtypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsopenTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['isOpen']) ? $data['isOpen'] : '');
        $list = $this->getIsopenList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    /**
     * 判断是否当前页面
     * @param $value
     * @param $data
     * @return bool
     */
    public function getIsActiveAttr($value, $data)
    {
        $url = request()->url();
        $thisV = \think\View::instance();
        $channel = isset($thisV->__NAV__)?collection($thisV->__NAV__)->toArray():[];
        if ((!empty($channel) && ($channel[0]['id'] == $data['id'] )) || $data['navUrl'] == $url) {
            return true;
        }
        return false;
    }

    /**
     * 获取导航列表
     * @param $tag
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getNavList($tag)
    {
        $type = empty($tag['type']) ? '' : $tag['type'];
        $typeid = !isset($tag['typeid']) ? '' : $tag['typeid'];
        $condition = empty($tag['condition']) ? '' : $tag['condition'];
        $field = empty($params['field']) ? '*' : $params['field'];
        $row = empty($tag['row']) ? 10 : (int)$tag['row'];
        $orderby = empty($tag['orderby']) ? 'weigh' : $tag['orderby'];
        $orderway = empty($tag['orderway']) ? 'asc' : strtolower($tag['orderway']);
        $limit = empty($tag['limit']) ? $row : $tag['limit'];
        $cache = !isset($tag['cache']) ? true : (int)$tag['cache'];
        $imgwidth = empty($tag['imgwidth']) ? '' : $tag['imgwidth'];
        $imgheight = empty($tag['imgheight']) ? '' : $tag['imgheight'];
        $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'asc';
        $cache = !$cache ? false : $cache;
        $where = [];
        $where['showis'] = 1;

        if ($type === 'top') {
            //顶级分类
            $where['navType'] = 0;
        } else {
            $where['navType'] = 1;
        }
        $order = $orderby == 'rand' ? 'rand()' : (in_array($orderby, ['weigh','id','createtime']) ? "{$orderby} {$orderway}" : "weigh {$orderway}");
        $list = self::where($where)
            ->where($condition)
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->cache($cache)
            ->select();
        return $list;
    }

}
